using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;
using ECommerce.Library.DataStructure;
using ECommerce.Library.Web;
using ECommerce.Library;
using Entity;
using eShop.Model;
using eShop.DataAccess;
using eShop.Business;

public partial class controls_wucCategoryLeftMenu : BaseControl
{
    private Category _subCategoryFor;
    public Category SubCategoryFor
    {
        get { return this._subCategoryFor; }
        set { this._subCategoryFor = value; }
    }

    private Category _parentCategoryFor;
    public Category ParentCategoryFor
    {
        get { return this._parentCategoryFor; }
        set { this._parentCategoryFor = value; }
    }
    public String PostId
    {
        get
        {
            try
            {
                return Page.RouteData.Values["partnerId"].ToString();
            }
            catch
            {
                return string.Empty;
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
        }
        LoadData();
    }

    private void LoadData()
    {
        if (!string.IsNullOrEmpty(PostId))
        {
            //ECommerce.DAL.CustomCollection<TblProductCategory> category = TblPartnerCategories.GetAllTblPartnerCategoryByPartnerId(Convert.ToInt32(PostId));
            Manufacturer brand = ManufacturerManager.GetByBrandUrl(PostId);
            if (brand != null)
            {
                rptCategoryLeftMenu.DataSource = CategoryManager.PartnerCategories(brand.ProductBrandId);
                rptCategoryLeftMenu.DataBind();
            }
        }
        else 
        {
            rptCategoryLeftMenu.DataSource = Category.RootCategories;
            rptCategoryLeftMenu.DataBind();

        }
    }

    protected void CategoryLeftMenu_DataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Category productCategory = (Category)e.Item.DataItem;
            string catId = "lnkCat" + productCategory.CategoryId;
            HtmlAnchor hlkParentCategory = (HtmlAnchor)e.Item.FindControl("lnkCat");

            if (hlkParentCategory != null)
            {
                if (!string.IsNullOrEmpty(PostId))
                    hlkParentCategory.HRef = "partner/"+PostId+"/"+productCategory.CategoryUrl;
                else
                hlkParentCategory.HRef = productCategory.CategoryUrl;
                if (ParentCategoryFor != null)
                {
                    if (productCategory.CategoryId == ParentCategoryFor.CategoryId)
                        hlkParentCategory.Attributes.Add("class", "menu-item-parent");
                }
            }

            SubCategoryFor = productCategory;
            if (SubCategoryFor != null)
            {
                ReadWriteCustomCollection<Category> subCategories = CategoryManager.GetSubCategories(SubCategoryFor);
                if (subCategories != null && productCategory != null && productCategory.CategoryId == SubCategoryFor.CategoryId)
                {
                    Repeater rptSubCategory = (Repeater)e.Item.FindControl("rptSubCategory");
                    if (rptSubCategory != null)
                    {
                        rptSubCategory.DataSource = subCategories;
                        rptSubCategory.DataBind();
                        rptSubCategory.Visible = true;
                    }
                }
            }
            else
            {

                ReadWriteCustomCollection<Category> subCategories = CategoryManager.GetSubCategories(productCategory);
                if (subCategories != null )
                {
                    Repeater rptSubCategory = (Repeater)e.Item.FindControl("rptSubCategory");
                    if (rptSubCategory != null)
                    {
                        rptSubCategory.DataSource = subCategories;
                        rptSubCategory.DataBind();
                        rptSubCategory.Visible = true;
                    }
                }
            }
        }
    }

    protected void rptSubCategory_DataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Category productCategory = (Category)e.Item.DataItem;
            HtmlAnchor hlkSubCategory = (HtmlAnchor)e.Item.FindControl("lnkChildCat");
            if (hlkSubCategory != null)
            {
                hlkSubCategory.Attributes.Add("name", "lnkChildCat" + productCategory.CategoryId);
                if (!string.IsNullOrEmpty(PostId))
                    hlkSubCategory.HRef = "partner/" + PostId + "/" + productCategory.CategoryUrl;
                else
                    hlkSubCategory.HRef = productCategory.CategoryUrl;
            }
        }
    }
}
